package appstate;

import com.jme3.scene.Spatial;
import control.character.AIControl;
import control.character.SimpleAIControl;
import java.util.ArrayList;
import java.util.List;

public class AISpace {
    protected List<AIControl> aiControls;

    public AISpace() {
	aiControls = new ArrayList<AIControl>();
    }

    public void update(float tpf) {
	for(AIControl control : aiControls) {
	    control.update(tpf);
	}
    }

    public void add(Object obj) {
	if (obj instanceof SimpleAIControl) {
            ((SimpleAIControl)obj).setAISpace(this);
	    aiControls.add((SimpleAIControl)obj);
        } else if (obj instanceof Spatial) {
            Spatial spatial = (Spatial)obj;
            SimpleAIControl control = spatial.getControl(SimpleAIControl.class);
            control.setAISpace(this);
	    aiControls.add(control);
        } else {
            throw (new UnsupportedOperationException("Cannot add this kind of object to the ai space."));
        }
    }

    public void remove(Object obj) {
	if (obj instanceof SimpleAIControl) {
            ((SimpleAIControl)obj).setAISpace(null);
	    aiControls.remove((SimpleAIControl)obj);
        } else if (obj instanceof Spatial) {
            SimpleAIControl control = ((Spatial)obj).getControl(SimpleAIControl.class);
            control.setAISpace(null);
	    aiControls.remove(control);
        } else {
            throw (new UnsupportedOperationException("Cannot remove this kind of object from the ai space."));
        }
    }
}
